package com.cksys.achievement.config.login.qq;

import com.cksys.achievement.controller.VerifyCodeController;
import com.cksys.achievement.entity.User;
import com.cksys.achievement.enums.SexEnum;
import com.cksys.achievement.exception.NoBindingException;
import com.cksys.achievement.exception.ValidateCodeException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @ClassName: QQBindingFilter
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2020/4/16 18:30
 */
public class QQBindingFilter extends AbstractAuthenticationProcessingFilter {

    public QQBindingFilter(AntPathRequestMatcher requestMatcher) {
        super(requestMatcher);
    }

    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException {
        HttpSession session = request.getSession();
        String unionId = (String) session.getAttribute("unionId");
        String sessionCode = (String) session.getAttribute(VerifyCodeController.SESSION_KEY);
        if (null == unionId) {
            throw new ValidateCodeException("参数错误！");
        }
        if (null == sessionCode) {
            throw new ValidateCodeException("验证码已失效！");
        }

        String flag = request.getParameter("flag");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String realName = request.getParameter("realName");
        String verificationCode = request.getParameter("verificationCode");
        if (null == flag || null == username || null == password || verificationCode == null) {
            throw new NoBindingException("参数错误！");
        }
        if (!sessionCode.equalsIgnoreCase(verificationCode)) {
            throw new ValidateCodeException("验证码错误！");
        }
        session.removeAttribute(VerifyCodeController.SESSION_KEY);

        User user = new User().setUsername(username).setPassword(password)
                .setAvatarPath("/images/logo.png").setQqId(unionId).setRealName(realName);

        String sex = request.getParameter("sex");
        if (null == sex) {
            sex = "SEX_MAN";
        }
        user.setSex(SexEnum.valueOf(sex));
        UsernamePasswordAuthenticationToken authenticationToken =
                new UsernamePasswordAuthenticationToken(user, flag);
        Authentication authenticate = getAuthenticationManager().authenticate(authenticationToken);
        if (authenticate != null) {
            return authenticate;
        }
        throw new NoBindingException("绑定失败！");
    }
}
